import ajax from '@/utils/request'

// 获取验证码
export const getRegCode = (phone) => {
    return ajax.get(`sms/codes/${phone}`)
}
// 用户注册
export const userRegsiter = (phone, code) => {
    return ajax.post('/authorizations', {
        data: { phone, code }
    })
}

// 用户登录
export const userLogin = (mobile, code) => {
    return ajax.post('/authorizations', {
        data: { mobile, code }
    })
}

// 获取所有channels
export const getChannels = () => {
    return ajax.get('/channels')
}
// 获取用户频道
export const getUserChannels = () => {
    return ajax.get('/user/channels')
}
// 编辑用户频道
export const editUserChannels = (channels) => {
    return ajax.put('/user/channels', {
        data: { channels }
    })
}
// 删除用户频道
export const deleteUserChannels = (channelId) => {
    return ajax.delete('/user/channels/' + channelId)
}
// 获取文章新闻推荐
export const getArticles = (id, preTimeStamp) => {
    return ajax.get('/articles', {
        data: {
            channel_id: id,
            timestamp: preTimeStamp
        }
    })
}
// 文章 - 不感兴趣
export const articleDislike = (id) => {
    return ajax.post('/article/dislikes', {
        data: {
            target: id
        }
    })
}
// 文章 - 举报文章
export const articleReport = (target, type) => {
    return ajax.post('/article/reports', {
        data: {
            target,
            type
        }
    })
}

// 搜索联想
export const searchAdvice = (q) => {
    return ajax.get('/suggestion/', {
        data: {
            q
        }
    })
}
// 搜索结果
export const getSearchResult = (q, page = 1, per_page = 10) => {
    return ajax.get('/search/', {
        data: {
            page,
            per_page,
            q
        }
    })
}

// 获取文章内容
export const getArticleContent = (id) => {
    return ajax.get('/articles/' + id)
}

// 关注用户
export const followUser = (id) => {
    return ajax.post('/user/followings', {
        data: {
            target: id
        }
    })
}
// 取消关注
export const unfollowUser = (id) => {
    return ajax.delete('/user/followings/' + id)
}

// 文章点赞
export const articleLike = (id) => {
    return ajax.post('/article/likings', {
        data: {
            target: id
        }
    })
}
// 收藏文章
export const articleFavorite = (id) => {
    return ajax.post('/article/collections', {
        data: {
            target: id
        }
    })
}
// 取消收藏文章
export const articleUnfavorite = (id) => {
    return ajax.delete('/article/collections/' + id)
}

// 取消点赞
export const articleUnLike = (id) => {
    return ajax.delete('/article/likings/' + id)
}

// 获取评论列表
export const getComments = (id, offset, limit = 10) => {
    const data = {
        type: 'a',
        source: id,
        offset,
        limit
    }
    if (!offset) delete data.offset
    return ajax.get('/comments', {
        data
    })
}
// 发表评论
export const postComment = (target, content, artId) => {
    const data = {
        target,
        content,
        artId
    }
    if (!artId) delete data.artId
    return ajax.post('/comments', { data })
}
// 对评论点赞
export const commentLike = (id) => {
    return ajax.post('/comment/likings', {
        data: {
            target: id
        }
    })
}
// 取消对评论点赞
export const commentUnLike = (id) => {
    return ajax.delete('/comment/likings/' + id)
}

// 获取用户信息
export const getUserInfo = () => {
    return ajax.get('/user/profile')
}

// 获取用户自己的资料
export const getUserProfile = () => {
    return ajax.get('/user')
}
// 编辑用户资料
export const editUserProfile = ({ name, gender, birthday, realName, intro }) => {
    const data = {}
    if (name) data.name = name
    if (gender) data.gender = gender
    if (birthday) data.birthday = birthday
    if (realName) data.real_name = realName
    if (intro) data.intro = intro
    return ajax.patch('/user/profile', {
        data
    })
}

// 编辑用户头像
export const editUserAvatar = (act) => {
    // 请求时设置Content-Type: multipart/form-data
    return ajax.patch('/user/photo', {
        data: {
            photo: act
        },
        headers: {
            'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryNwAX5yeMnmF6R8Tf'
        }
    })
}
